package test;

import graphSample.SerialMachine.*;

import core.Graph;
import core.GraphBalancer;
import core.Node;
import core.NodeScanner;

public class BatchRun extends Thread{
	
	public static void main(String[] argv){
		NodeScanner ns = new NodeScanner();
		ns.start();
		
		try {
			sleep(10000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		
		ns.setDone();
		
		Node.printNodeList();
		Node.printNodeDist();
		
		// 记录当前所有可用节点
		
		//SerialMachine model;
		Graph g;
		ThroughputRecord tr = new ThroughputRecord();
		
		/**
		 * Group 1
		 */
		//g = new SerialMachineOC(1,new int[]{2,2,2,2},new double[]{18,18,18,18,18},2).g;
		//run(g, 3000000, 10000); 
		//tr.cal("SerialMachineOC",g.getEntityByName("p_store").tokenPool);
		
		//g = new SerialMachineOR(0,new int[]{2,2,2,2},new double[]{18,18,18,18,18},2).g;
		//run(g, 1200000, 10000); 
		//tr.cal("SerialMachineOR",g.getEntityByName("p_store").tokenPool);
		
		g = new SerialMachineTC(1,new int[]{2,2,2,2},new double[]{18,18,18,18,18},2).g;
		run(g, 3000000, 10000); 
		tr.cal("SerialMachineTC",g.getEntityByName("p_store").tokenPool);
		
		//g = new SerialMachineTR(0,new int[]{2,2,2,2},new double[]{18,18,18,18,18},2).g;
		//run(g, 1200000, 10000); 
		//tr.cal("SerialMachineTR",g.getEntityByName("p_store").tokenPool);
		
		
		/**
		 * Group 2
		 */
		
		g = new SerialMachineOC(2,new int[]{30,30,30,30},new double[]{90,90,90,90,90},10).g;
		run(g, 3000000, 10000); 
		tr.cal("SerialMachineOC",g.getEntityByName("p_store").tokenPool);
		
		///g = new SerialMachineOR(0,new int[]{30,30,30,30},new double[]{90,90,90,90,90},10).g;
		//run(g, 1200000, 10000); 
		//tr.cal("SerialMachineOR",g.getEntityByName("p_store").tokenPool);
		
		g = new SerialMachineTC(2,new int[]{30,30,30,30},new double[]{90,90,90,90,90},10).g;
		run(g, 3000000, 10000); 
		tr.cal("SerialMachineTC",g.getEntityByName("p_store").tokenPool);
		
		//g = new SerialMachineTR(0,new int[]{30,30,30,30},new double[]{90,90,90,90,90},10).g;
		//run(g, 1200000, 10000); 
		//tr.cal("SerialMachineTR",g.getEntityByName("p_store").tokenPool);
		
		/**
		 * Group 3
		 */
		
		//g = new SerialMachineOC(3,new int[]{6,6,6,6},new double[]{18,11.3333,8,6,4.6667},2).g;
		//run(g, 1200000, 10000); 
		//tr.cal("SerialMachineOC",g.getEntityByName("p_store").tokenPool);
		
		//g = new SerialMachineOR(0,new int[]{6,6,6,6},new double[]{18,11.3333,8,6,4.6667},2).g;
		//run(g, 1200000, 10000); 
		//tr.cal("SerialMachineOR",g.getEntityByName("p_store").tokenPool);
		
		//g = new SerialMachineTC(3,new int[]{6,6,6,6},new double[]{18,11.3333,8,6,4.6667},2).g;
		//run(g, 1200000, 10000); 
		//tr.cal("SerialMachineTC",g.getEntityByName("p_store").tokenPool);
		
		//g = new SerialMachineTR(0,new int[]{6,6,6,6},new double[]{18,11.3333,8,6,4.6667},2).g;
		//run(g, 1200000, 10000); 
		//tr.cal("SerialMachineTR",g.getEntityByName("p_store").tokenPool);
		
		/**
		 * Group 4
		 */
		
		//g = new SerialMachineOC(4,new int[]{10,10,10,10},new double[]{90,56.6667,23.3333,56.6667,90},10).g;
		//run(g, 1200000, 10000); 
		//tr.cal("SerialMachineOC",g.getEntityByName("p_store").tokenPool);
		
		//g = new SerialMachineOR(0,new int[]{10,10,10,10},new double[]{90,56.6667,23.3333,56.6667,90},10).g;
		//run(g, 1200000, 10000); 
		//tr.cal("SerialMachineOR",g.getEntityByName("p_store").tokenPool);
		
		//g = new SerialMachineTC(4,new int[]{10,10,10,10},new double[]{90,56.6667,23.3333,56.6667,90},10).g;
		//run(g, 1200000, 10000); 
		//tr.cal("SerialMachineTC",g.getEntityByName("p_store").tokenPool);
		
		//g = new SerialMachineTR(0,new int[]{10,10,10,10},new double[]{90,56.6667,23.3333,56.6667,90},10).g;
		//run(g, 1200000, 10000); 
		//tr.cal("SerialMachineTR",g.getEntityByName("p_store").tokenPool);
		
		
		/**
		 * Group 5
		 */
		
		//g = new SerialMachineOC(5,new int[]{30,30,30,30},new double[]{30,30,190,30,30},10).g;
		//run(g, 1200000, 10000); 
		//tr.cal("SerialMachineOC",g.getEntityByName("p_store").tokenPool);
		
		//g = new SerialMachineOR(0,new int[]{30,30,30,30},new double[]{30,30,190,30,30},10).g;
		//run(g, 1200000, 10000); 
		//tr.cal("SerialMachineOR",g.getEntityByName("p_store").tokenPool);
		
		//g = new SerialMachineTC(5,new int[]{30,30,30,30},new double[]{30,30,190,30,30},10).g;
		//run(g, 1200000, 10000); 
		//tr.cal("SerialMachineTC",g.getEntityByName("p_store").tokenPool);
		/*
		g = new SerialMachineTR(0,new int[]{30,30,30,30},new double[]{30,30,190,30,30},10).g;
		run(g, 1200000, 10000); 
		tr.cal("SerialMachineTR",g.getEntityByName("p_store").tokenPool);
		*/
		
	}
	
	static void run(Graph g, int maxTime, int scanInterval){
		
		System.out.println("...Print graph brief info");
		g.printNameAndId();
		
		System.out.println("...Analysis this graph");
		// Analysis this graph
		g.anls();
		g.printCluster();
		g.printWorkload();
		g.printRela();
		
		System.out.println("...Produce assignment plan");
		// Produce assignment plan
		g.preAssign();
		g.printPreAssignment();
		
		System.out.println("...Reset all the Nodes");
		// Reset all the Nodes
		Node.resetAll();
		
		System.out.println("...Assign the Entities and Put the initial Tokens");
		// Assign the Entities and Put the initial Tokens
		g.assignAndImplAllEntities();
		
		System.out.println("...Implement all Process");
		// Implement all Process
		g.implAllProc();
		
		// TODO: Test the Graph Balancer
		GraphBalancer gb = new GraphBalancer(g,scanInterval);
		gb.start();
			
		System.out.println("...Waiting sometime");
		try {
			sleep(maxTime);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		gb.setDone();
		try {
			sleep(scanInterval*3);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		
		System.out.println("...Unload All Process");
		// Unload All Process
		g.unloadAllProc();
		
		System.out.println("...Get Evaluation Results");
		// Get Evaluation Results
		g.getResult();
		
		System.out.println("...Unload all Entities and Generate results");
		// Unload all Entities and generate results
		g.unloadAllEntities();
		g.printResultToFile();
		
		System.out.println("...Done");
	}
}
